Dynamically Read, Compile, and Run Source Code From a Text File 您所在的位置:网站首页 generating and compiling source code from a codedom Dynamically Read, Compile, and Run Source Code From a Text File

Dynamically Read, Compile, and Run Source Code From a Text File

2024-06-24 20:44| 来源: 网络整理| 查看: 265

hie

thanks for the code on the "cParams.ReferencedAssemblies.Add" howver im facing problems on adding an an adding an office microsoft project reference, it says it cannot find  "Microsoft.Office.Interop.MSProject.dll"

i had referenced it

here's e code

string input = "part=task.Text11;";

                           //sReader.ReadToEnd();

                           // sReader.Close();

                           // Code literal  

                           string code =

                               @"using System;  

                 using System.Windows.Forms;  

 using Microsoft.Office.Interop.MSProject;

                 namespace DCDAddOn

                 {  

                     public class Transform  

                     {  

                          public void UpdateText(Microsoft.Office.Interop.MSProject.Task task, string part)  

                          {" + input + @"  

                          }  

                      }  

                  }";

                           // Compile code  

                           string code2 = "textBox1.Text=Label1.Text;";

 CSharpCodeProvider cProv = new CSharpCodeProvider();

 CompilerParameters cParams = new CompilerParameters();

cParams.ReferencedAssemblies.Add("mscorlib.dll");

cParams.ReferencedAssemblies.Add("System.dll");

cParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");

cParams.ReferencedAssemblies.Add("Microsoft.Office.Interop.MSProject.dll");

cParams.GenerateExecutable = false;

 cParams.GenerateInMemory = true;

                           CompilerResults cResults = cProv.CompileAssemblyFromSource(cParams, code);

                           // Check for errors  

                           if (cResults.Errors.Count != 0) {

                               foreach (var er in cResults.Errors) {

                                   MessageBox.Show(er.ToString());

                               }

                           } else {

                               // Attempt to execute method.  

                               object obj = cResults.CompiledAssembly.CreateInstance("DCDAddOn.Transform");

                               Type t = obj.GetType();

                               object[] arg = {task,part }; // Pass our textbox to the method  

                               t.InvokeMember("UpdateText", BindingFlags.InvokeMethod, null, obj, arg);

                           }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有